package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.DeviceType;
import com.cloudfun.campusshare.common.constant.TokenType;
import com.cloudfun.campusshare.common.model.vo.*;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.service.DeviceOpServiceDelegate;
import com.cloudfun.campusshare.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * Created by Huqin on 2020/6/1
 */
@RestController
// TODO 修改
@RequestMapping("/v1/device")
@Api(value = "平台商操作设备模块")
@Validated
@Slf4j
@AuthRequired
public class PMDeviceOpController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceOpServiceDelegate deviceOpService;

    @ApiOperation(value = "绑定设备", notes = "绑定设备")
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> bindDevice(@ApiParam(value = "DeviceVO", required = true) @Valid @RequestBody DeviceVO deviceVo) {
        log.debug("平台商：{}绑定设备：{}", AuthenticationHolder.getUserId(), deviceVo);
        BaseResult<?> baseResult = deviceService.merchantBind(deviceVo);
        return baseResult;
    }

    @ApiOperation(value = "解绑设备", notes = "解绑设备")
    @RequestMapping(value = "/unbind", method = RequestMethod.GET)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> unbindDevice(@ApiParam(value = "设备id")
                                      @NotEmpty(message = "设备Id不能为空")
                                      @Length(max = 40, message = "设备Id过长，限制40个字")
                                      @RequestParam(value = "deviceId") String imei) {
        log.debug("平台商：{}解绑设备：{}", AuthenticationHolder.getUserId(), imei);
        return deviceService.unbind(imei);
    }

    @ApiOperation(value = "洗涤操作", notes = "洗涤操作")
    @RequestMapping(value = "/operate", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> wash(
            @ApiParam(value = "deviceControlVo", required = true) @Valid @RequestBody RollerWasherOpVO rollerWasherOpVo) {
        log.debug("平台商：{}洗涤操作：{}", AuthenticationHolder.getUserId(), rollerWasherOpVo);
        return deviceOpService.wash(rollerWasherOpVo);
    }

    @ApiOperation(value = "复位操作", notes = "复位操作")
    @RequestMapping(value = "/reset", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> reset(@ApiParam(value = "deviceResetVo", required = true) @Valid @RequestBody DeviceResetVO deviceResetVo) {
        log.debug("平台商：{}复位操作：{}", AuthenticationHolder.getUserId(), deviceResetVo);
        return deviceOpService.reset(deviceResetVo.getImei());
    }

    @ApiOperation(value = "烘干操作", notes = "烘干操作")
    @RequestMapping(value = "/drying", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> drying(@ApiParam(value = "deviceDryingVO", required = true) @Valid @RequestBody DryerOperateVO deviceDryingVO) {
        log.debug("平台商：{}烘干操作：{}", AuthenticationHolder.getUserId(), deviceDryingVO);
        if (deviceDryingVO.getDeviceType().equals(DeviceType.GGY8.getName()) && deviceDryingVO.getTempLevel().equals(1)) {
            return BaseResultFactory.produceResult(Code.PARAMETER_ERROR, "温度等级错误");
        }

        if(deviceDryingVO.getDeviceType().equals(DeviceType.GGY6.getName()) && (deviceDryingVO.getTempLevel().equals(1)
                || deviceDryingVO.getTempLevel().equals(3))){
            return BaseResultFactory.produceResult(Code.PARAMETER_ERROR, "温度等级错误");
        }
        return deviceOpService.dry(deviceDryingVO);
    }

    @ApiOperation(value = "波轮洗涤操作", notes = "波轮洗涤操作")
    @RequestMapping(value = "/pulseWash", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> pulseWash(@ApiParam(value = "operateVO", required = true) @Valid @RequestBody PulseWasherOperateVO operateVO) {
        log.debug("平台商：{}波轮洗涤操作：{}", AuthenticationHolder.getUserId(), operateVO);
        return deviceOpService.pulseWash(operateVO);
    }

    @ApiOperation(value = "洗鞋操作", notes = "洗鞋操作")
    @RequestMapping(value = "/shoeWash", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> shoeWash(@ApiParam(value = "operateVO", required = true) @Valid @RequestBody ShoeWasherOperateVO operateVO) {
        log.debug("平台商：{}洗鞋操作：{}", AuthenticationHolder.getUserId(), operateVO);
        return deviceOpService.shoeWash(operateVO);
    }

    @ApiOperation(value = "查询单个属性操作", notes = "查询单个属性操作")
    @RequestMapping(value = "/getPropertyByName", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    BaseResult<?> getPropertyByName(@ApiParam(value = "deviceQueryVo", required = true) @Valid @RequestBody DevicePropertyQueryVO devicePropertyQueryVo) {
        log.debug("平台商：{}查询单个属性操作：{}", AuthenticationHolder.getUserId(), devicePropertyQueryVo);
        return deviceOpService.getPropertyByName(devicePropertyQueryVo.getImei(), devicePropertyQueryVo.getProperty());
    }

    @ApiOperation(value = "查询多个属性操作", notes = "查询多个属性操作")
    @RequestMapping(value = "/getProperties", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    BaseResult<?> getProperties(@ApiParam(value = "devicePropertiesQueryVO", required = true) @Valid @RequestBody DevicePropertiesQueryVO devicePropertiesQueryVO) {
        log.debug("平台商：{}查询多个属性操作：{}", AuthenticationHolder.getUserId(), devicePropertiesQueryVO);
        return deviceOpService.getProperties(devicePropertiesQueryVO.getImei());
    }

    @ApiOperation(value = "查询告警操作", notes = "查询告警操作")
    @RequestMapping(value = "/getCautionList", method = RequestMethod.GET)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> getCautionList(@ApiParam(value = "设备Id", required = true)
                                        @NotEmpty(message = "设备Id不能为空")
                                        @Length(max = 40, message = "设备Id过长，限制40个字")
                                        @RequestParam("deviceId") String imei) {
        log.debug("平台商：{}查询告警操作：{}", AuthenticationHolder.getUserId(), imei);
        return deviceOpService.getCautionList(imei);
    }

    @ApiOperation(value = "查询设备是否在线操作", notes = "查询设备是否在线操作")
    @RequestMapping(value = "/isOnline", method = RequestMethod.GET)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> isOnline(@ApiParam(value = "设备Id", required = true)
                                  @NotEmpty(message = "设备Id不能为空")
                                  @Length(max = 40, message = "设备Id过长，限制40个字")
                                  @RequestParam("deviceId") String imei) {
        log.debug("平台商：{}查询设备是否在线操作：{}", AuthenticationHolder.getUserId(), imei);
        return deviceOpService.deviceOnline(imei);
    }

    @ApiOperation(value = "发送设备验证码", notes = "发送设备验证码")
    @RequestMapping(value = "/verifyCode", method = RequestMethod.POST)
    @AuthRequired(type = TokenType.TOOLKIT)
    public BaseResult<?> verifyCode(@ApiParam(value = "verifyCodeVO", required = true) @Valid @RequestBody DeviceVerifyCodeVO verifyCodeVO) {
        log.debug("平台商：{}发送设备验证码：{}", AuthenticationHolder.getUserId(), verifyCodeVO);
        return deviceOpService.deviceVerifyCode(verifyCodeVO.getImei(), verifyCodeVO.getVerifyCode());
    }

    @ApiOperation(value = "添加洗衣液", notes = "添加洗衣液")
    @RequestMapping(value = "/pumpStart", method = RequestMethod.POST)
    public BaseResult<?> pumpStart(@ApiParam(value = "加液泵设置时间") @Valid @RequestBody PumpWorkVO pumpWorkVO) {
        log.debug("平台商：{}添加洗衣液操作：{}", AuthenticationHolder.getUserId(), pumpWorkVO);
        return deviceOpService.pumpStart(pumpWorkVO);
    }
}
